package com.imooc.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.config.SpringFoxConfig;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.dao.CategoryMapper;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.request.AddCategoryAdd;
import com.imooc.mall.model.request.UpdateCategoryUpdate;
import com.imooc.mall.model.vo.CategoryVO;
import com.imooc.mall.service.CategoryService;

import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public void add(AddCategoryAdd addCategoryAdd){
        Category category = new Category();
        //BeanUtils 实现用户复制的功能
        BeanUtils.copyProperties(addCategoryAdd,category);
        // 查询数据库中是否包含这个对象
        Category oldCategory = categoryMapper.selectByName(category.getName());
        if (oldCategory != null){
            // 实际上 remin exception 就是让异常没有去捕获到 不需要
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = categoryMapper.insertSelective(category);
        if(count == 0){
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }
    }

    @Override
    public void UpdateCategory(Category updateCategory){

        if(updateCategory.getName()!= null){
            Category categoryOld = categoryMapper.selectByName(updateCategory.getName());
            // 在查到数据库的数据 和传入的用户名字是不是一样
            if(categoryOld != null && !categoryOld.getId().equals(updateCategory.getId())){
                throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
            }
        }
        int count = categoryMapper.updateByPrimaryKeySelective(updateCategory);
        if (count==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void delete(Integer id){
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        // 查不到记录无法删除，删除失败
        if (categoryOld == null){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if (count == 0){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    /*分页查询的*/
    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize,"type, order_num");
        List<Category> categoryList = categoryMapper.selectList();
        PageInfo PageInfo = new PageInfo(categoryList);
        return PageInfo;
    }

    /**/
    @Override  //key值
    @Cacheable(value = "listCategoryForCustomer")
    public List<CategoryVO> listCategoryForCustomer(Integer parentId){
        ArrayList<CategoryVO> categoryVoList = new ArrayList<>();
        // 函数的作用就是对这个对象的属性什么的进行填充
        recursivelyFindCategories(categoryVoList,parentId);
        return categoryVoList;
    }

    private void recursivelyFindCategories(List<CategoryVO> categoryVoList, Integer parentId){
        // 递归获取所有子类别，并且组成一个“目录树”
        List<Category> categories = categoryMapper.selectCategoriesByParentId(parentId);
        if (!CollectionUtils.isEmpty(categories)) {
            for (int i = 0; i < categories.size(); i++) {
                Category category = categories.get(i);
                CategoryVO categoryVO = new CategoryVO();
                //除了新增的其他的都没有
                BeanUtils.copyProperties(category,categoryVO);
                // 传进来的列表的作用就是想操作他，往里面添加数据
                categoryVoList.add(categoryVO);
                recursivelyFindCategories(categoryVO.getChildCategory(),categoryVO.getId());
            }
        }
    }


}